package com.bootdo.web.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.work.domain.CarDO;
import com.bootdo.work.domain.OrderDO;
import com.bootdo.work.service.CarService;
import com.bootdo.work.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author admin
 * @email 1992lcg@163.com
 * @date 2021-05-09 21:37:16
 */
 
@Controller
@RequestMapping("/web")
public class WebController extends BaseController {
	@Autowired
	private CarService carService;
	@Autowired
	private OrderService orderService;
	
	@GetMapping()
	String Car(Model model){
		model.addAttribute("userid", getUser().getUserId());
		model.addAttribute("username", getUser().getUsername());
		return "web/main";
	}

	@ResponseBody
	@GetMapping("/weblist")
	/*@RequiresPermissions("work:car:car")*/
	public List listcar(CarDO carDO){
		List<CarDO> carList = carService.weblist(carDO);
		return carList;
	}

	@GetMapping("/rent")
	String toRent(Integer carid,Model model){
		CarDO car = carService.get(carid);
		model.addAttribute("car", car);
		model.addAttribute("userid", getUser().getUserId());
		return "web/rent";
	}


	@ResponseBody
	@GetMapping("/list")
	/*@RequiresPermissions("work:car:car")*/
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CarDO> carList = carService.list(query);
		int total = carService.count(query);
		PageUtils pageUtils = new PageUtils(carList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	/*@RequiresPermissions("work:car:add")*/
	String add(){
	    return "work/car/add";
	}

	@GetMapping("/edit/{id}")
	/*@RequiresPermissions("work:car:edit")*/
	String edit(@PathVariable("id") Integer id,Model model){
		CarDO car = carService.get(id);
		model.addAttribute("car", car);
	    return "work/car/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	/*@RequiresPermissions("work:car:add")*/
	public R save( OrderDO orderDO){
		Date date = new Date();
		orderDO.setInDate(date);
		if(orderService.save(orderDO)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	/*@RequiresPermissions("work:car:edit")*/
	public R update( CarDO car){
		carService.update(car);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	/*@RequiresPermissions("work:car:remove")*/
	public R remove( Integer id){
		if(carService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	/*@RequiresPermissions("work:car:batchRemove")*/
	public R remove(@RequestParam("ids[]") Integer[] ids){
		carService.batchRemove(ids);
		return R.ok();
	}
	
}
